#define GL_SILENCE_DEPRECATION

#import "AppDelegate.h"
#import "OpenGLView.h"
@implementation AppDelegate
@synthesize cfg;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSRect frame = CGRectMake(0, 0, self.cfg.screenWidth, self.cfg.screenHeight);
    NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable;
    
    self.window = [[NSWindow alloc] initWithContentRect:frame styleMask:style backing:NSBackingStoreBuffered defer:YES];
    self.window.title = [NSString stringWithCString:self.cfg.appName encoding:NSUTF8StringEncoding];
    self.window.backgroundColor = [NSColor redColor];

    // 窗口居中
    [self.window center];
    // 窗口显示
    [self.window makeKeyAndOrderFront:self];

    // 
    NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = {
            NSOpenGLPFAOpenGLProfile,   NSOpenGLProfileVersion3_2Core,
            NSOpenGLPFAColorSize,       32,
            NSOpenGLPFADepthSize,       16,
            NSOpenGLPFAAlphaSize,       8,
            NSOpenGLPFADoubleBuffer,
            NSOpenGLPFAAccelerated,
            NSOpenGLPFANoRecovery,
            0
    };
    NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes];
    if (format == nil) {
        NSLog(@"Faild create pixel format!");
        return;
    }
     
    // initialize the view
    OpenGLView *view = [[OpenGLView alloc] initWithFrame:self.window.frame];
    [self.window setContentView:view];

}


- (void)applicationWillTerminate:(NSNotification *)aNotification {
    // Insert code here to tear down your application
}

- (nullable NSMenu *)applicationDockMenu:(NSApplication *)sender {
    NSMenu* menu = [[NSMenu alloc] initWithTitle:@"Menu"];
    NSMenuItem* item1 = [[NSMenuItem alloc] initWithTitle:@"菜单1" action:@selector(click) keyEquivalent:(@"")];
    item1.target = self;
    
    NSMenuItem* item2 = [[NSMenuItem alloc] initWithTitle:@"菜单2" action:@selector(click) keyEquivalent:(@"")];
    item2.target = self;
    
    NSMenuItem* item3 = [[NSMenuItem alloc] initWithTitle:@"菜单3" action:@selector(click) keyEquivalent:(@"")];
    item3.target = self;
    
    NSMenu* subMenu = [[NSMenu alloc] initWithTitle:@"subMenu"];
    
    NSMenuItem* item4 = [[NSMenuItem alloc] initWithTitle:@"菜单4" action:@selector(click) keyEquivalent:(@"")];
    item4.target = self;
    
    [subMenu addItem:item4];
    [menu addItem:item1];
    [menu addItem:item2];
    [menu addItem:item3];
 
    [menu setSubmenu:subMenu forItem:item3];
    
    return menu;
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
    NSLog(@"AppDelegate applicationShouldTerminateAfterLastWindowClosed");
    return TRUE;
}

- (void) click {
    NSLog(@"click");
}

@end
